package com.yuexiao.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuexiao.config.RedisUtil;
import com.yuexiao.mapper.EmployeeMapper;
import com.yuexiao.pojo.Employee;
import com.yuexiao.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author Administrator
 * @description 针对表【employee(员工信息)】的数据库操作Service实现
 * @createDate 2022-08-10 22:43:58
 */
@Component
@Service(interfaceClass = EmployeeService.class)
@Transactional
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
        implements EmployeeService {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 根据用户名查询员工对象
     * @param username String类型 用户名
     * @return Employee 员工对象
     */
    @Override
    public Employee getEmployeeByUsername(String username) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Employee::getUsername, username);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件分页查询员工列表数据
     *
     * @param page   int类型  当前页
     * @param pageSize int类型 每页显示条数
     * @param name  String类型   员工姓名
     * @return Page mybatis-plus中的分页对象
     */
    @Override
    public Page getEmployeeByPages(int page, int pageSize, String name) {
        //查询redis看是否有缓存数据，如果有则直接取出返回
        Page employees = (Page) redisUtil.hget("employees", page + "_" + pageSize + "_" + name);
        if (employees != null) {
            return employees;
        }
        //redis中没有数据，查询数据库
        Page queryPage = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //如果传入的姓名字段不为空则拼装查询条件
        if (name != "" && name != "null" && !Objects.isNull(name)) {
            queryWrapper.like(Employee::getName, name);
        }
        queryWrapper.ne(Employee::getRole, "ROLE_ADMIN");
        Page page1 = baseMapper.selectPage(queryPage, queryWrapper);
        //redis中只存储分页信息，不存储带有查询条件的，所以进行判断
        if ("".equals(name) || "null".equals(name) || Objects.isNull(name)) {
            //如果查询条件为空  并且查询结果不为空则存储
            if(page1!=null&&page1.getTotal()>0){
                redisUtil.hset("employees", page + "_" + pageSize + "_" + name, page1);
            }
        }
        return page1;
    }

    /**
     * 新增员工信息
     * @param employee Employee 员工对象
     * @return boolean true:成功 false:失败
     */
    @Override
    public boolean save(Employee employee) {
        int insert = baseMapper.insert(employee);
        if(insert>0){
            //如果添加成功，重置redis中员工表数据
            redisUtil.del("employees");
            return true;
        }
        return false;
    }

    /**
     * 根据id查询员工信息
     * @param id Long 员工主键
     * @return Employee 员工对象
     */
    @Override
    public Employee getEmployeeById(Long id) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Employee>().eq(Employee::getId,id));
    }


    /**
     * 更新员工信息
     * @param employee Employee 员工对象
     * @return boolean true:false
     */
    @Override
    public boolean update(Employee employee) {
        System.out.println(employee);
        int i = baseMapper.updateById(employee);
        if(i>0){
            redisUtil.del("employees");
            return true;
        }
        return false;
    }
}




